package cn.ekgc.system.service.impl;

import cn.ekgc.system.dao.MenuDao;
import cn.ekgc.system.dao.UserDao;
import cn.ekgc.system.pojo.User;
import cn.ekgc.system.pojo.dto.MenuDTO;
import cn.ekgc.system.pojo.vo.MenuTree;
import cn.ekgc.system.service.MenuService;
import cn.ekgc.system.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    /**
     * 解析token返回用户信息
     * @param token值转换后的object
     * @return 登录的用户
     */
    @Override
    public User analysisUser(Object o) {
        Map<String,Object> oo = JSON.parseObject(JSON.toJSONString(o), Map.class);
        Map<String,Object> oo1 = JSON.parseObject(JSON.toJSONString(oo.get("user")), Map.class);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        User u = new User();
        u.setId((Integer) oo1.get("id"));
        u.setUserName((String) oo1.get("userName"));
        u.setPassword((String) oo1.get("password"));
        u.setArea((String) oo1.get("area"));
        u.setAge((Integer) oo1.get("age"));
        u.setAddress((String) oo1.get("address"));
        u.setRole((Integer) oo1.get("role"));
        u.setPhone((String) oo1.get("phone"));
        u.setSex((Integer) oo1.get("sex"));
        u.setCreatedTime(LocalDateTime.parse((String)oo1.get("createdTime"),df));
        u.setEntryTime(LocalDateTime.parse((String)oo1.get("entryTime"),df));
        u.setJobId((Integer) oo1.get("jobId"));
        u.setWorkStatus((Integer) oo1.get("workStatus"));
        u.setUpdatedTime(LocalDateTime.parse((String)oo1.get("updatedTime"),df));
        return u;
    }

    // todo
}
